package cn.edu.zafu.bigdata.x.Foo;

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) throws IOException {
        int port = 3000; // 服务端端口
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Server is running and waiting for client connection...");

        try {
            while (true) {
                Socket clientSocket = serverSocket.accept();
                System.out.println("Client connected.");

                // 创建输入输出流
                BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

                // 读取客户端发送的消息
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    System.out.println("Client says: " + inputLine);

                    // 回复客户端
                    String response = "Server received your message: " + inputLine;
                    out.println(response);
                }
                clientSocket.close();
            }
        } finally {
            serverSocket.close();
        }
    }
}
